package oylj.recycle.all_in_one.camera;

import static oylj.recycle.all_in_one.Utils.createInnerImg;

import android.content.Context;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;

import com.company.NetSDK.INetSDK;
import com.company.NetSDK.SDK_RealPlayType;
import com.company.PlaySDK.Constants;
import com.company.PlaySDK.IPlaySDK;

import java.util.Objects;

import oylj.recycle.all_in_one.SystemConfig;

public final class DeviceLib {
    private static final String TAG = "DeviceLib";
    private final Context mContext;
    private final String DA_HUA="1",HAI_KANG="2";

    //登录句柄和预览句柄 大华成功是很大的负数 失败是0  海康成功是0 失败-1
    private long loginHandle;
    private long realHandle;
    private final SurfaceView mSurfaceView;
//    private final ImageView mImageView;
    int mPlayPort = IPlaySDK.PLAYGetFreePort();
    private final int RAW_AUDIO_VIDEO_MIX_DATA = 0;
    private final SystemConfig.Camera mCameraConfig;

    public DeviceLib(Context context, SurfaceView surfaceView,SystemConfig.Camera cameraConfig) {
        mContext = context;
//        mImageView = imageView;
        mSurfaceView = surfaceView;
        mCameraConfig = cameraConfig;
        login();
    }

    public boolean initSuccess() {
        Log.d(TAG, "initSuccess: realHandle "+realHandle+" mCameraConfig.getType() "+mCameraConfig.getType());
        if(Objects.equals(mCameraConfig.getType(), DA_HUA)){
            return realHandle < 0;
        }
        if(Objects.equals(mCameraConfig.getType(), HAI_KANG)){
            return realHandle >= 0;
        }
        return false;
    }
    public void login() {
        try{
            Log.d(TAG, String.format("%s  %s  %s %s %s",mCameraConfig.getIp(),mCameraConfig.getPassword(),mCameraConfig.getType(),DA_HUA,HAI_KANG));
            if(Objects.equals(mCameraConfig.getType(), DA_HUA)){
                IPlaySDK.InitSurface(mPlayPort,mSurfaceView);
                IPLoginModule mLoginModule = new IPLoginModule();
                mLoginModule.login(mCameraConfig.getIp(),"37777","admin",mCameraConfig.getPassword());
                loginHandle = mLoginModule.getLoginHandle();
            }
        }catch (Exception e){
            Log.d(TAG, "login: ",e);
        }
    }

    public void stopRealPlay(){
        if(Objects.equals(mCameraConfig.getType(), DA_HUA)) {
            if(realHandle == 0) {
                return;
            }

            try {
                IPlaySDK.PLAYStop(mPlayPort);
                IPlaySDK.PLAYCloseStream(mPlayPort);
                INetSDK.StopRealPlayEx(realHandle);
                IPlaySDK.PLAYRefreshPlay(mPlayPort);
//                mImageView.postDelayed(() -> {
//                    mImageView.setVisibility(View.VISIBLE);
//                }, 2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            realHandle = 0;
        }
    }

    public boolean prePlay(SurfaceView sv){
        int STREAM_BUF_SIZE = 1024 * 1024 * 2;
        boolean isOpened = IPlaySDK.PLAYOpenStream(mPlayPort, null, 0, STREAM_BUF_SIZE) != 0;
        if(!isOpened) {
            Log.d(TAG,"OpenStream Failed");
            return false;
        }
        boolean isPlayin = IPlaySDK.PLAYPlay(mPlayPort, sv) != 0;
        if (!isPlayin) {
            Log.d(TAG,"PLAYPlay Failed");
            IPlaySDK.PLAYCloseStream(mPlayPort);
            return false;
        }
        return true;
    }




    public void startPlay(){
        Log.d(TAG, "startPlay: loginHandle "+loginHandle+" surfaceView valid "+mSurfaceView.getHolder().getSurface().isValid());
        if(Objects.equals(mCameraConfig.getType(), DA_HUA)) {
            //SDK_RType_Realplay_1 辅 SDK_RType_Realplay_0 主
            realHandle = INetSDK.RealPlayEx(loginHandle, 0, SDK_RealPlayType.SDK_RType_Realplay_1);
            Log.d(TAG, "startPlay: realHandle "+realHandle);
            if (realHandle == 0) {
                Log.d(TAG, "initCamera: RealPlayEx failed!");
                return;
            }
            if (!prePlay(mSurfaceView)) {
                Log.d(TAG, "prePlay returned false..");
                INetSDK.StopRealPlayEx(realHandle);
                return;
            }
            INetSDK.SetRealDataCallBackEx(realHandle,
                    (rHandle, dataType, buffer, bufSize, param) -> {
                        if (RAW_AUDIO_VIDEO_MIX_DATA == dataType) {
                            IPlaySDK.PLAYInputData(mPlayPort, buffer, buffer.length);
//                            mImageView.post(() -> {
//                                mImageView.setVisibility(View.GONE);
//                            });
                        }
                    }
                    , 1);
        }


    }
    public String capture(){
        String filename = createInnerImg(mContext);
        String res = "";
        if(Objects.equals(mCameraConfig.getType(), DA_HUA)) {
            if (realHandle != 0) {
                if (IPlaySDK.PLAYCatchPicEx(mPlayPort, filename, Constants.PicFormat_JPEG) != 0) {
                    res =  filename;
                }
            }
        }
        return  res;
    }

}
